package com.lg.wxq.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

import com.lg.wxq.config.AppCode;
import com.lg.wxq.config.SharedPreferencesConfig;
import com.lg.wxq.config.Strings;
import com.lg.wxq.logic.MainService;
import com.lg.wxq.utils.AnimationUtils;
import com.lg.wxq.utils.DebugUtils;
import com.lg.wxq.utils.NetUtil;
import com.lg.wxq.utils.StringUtils;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

public class LogoActivity extends Activity {
	private View logo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logo);
		logo = findViewById(R.id.logo_im_logo);
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (NetUtil.checkNet(LogoActivity.this)) {
			logo.startAnimation(AnimationUtils.getAlphaAnimation(0.1f, 1.0f, 2000, new AnimationListener() {

				public void onAnimationStart(Animation animation) {
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationEnd(Animation animation) {
					// 开启服务
					if (!MainService.serviceState) {
						Intent intent = new Intent(LogoActivity.this, MainService.class);
						LogoActivity.this.startService(intent);
					}
					// 开启微博
					Weibo weibo = Weibo.getInstance();
					weibo.setupConsumerConfig(AppCode.CONSUMER_KEY, AppCode.CONSUMER_SECRET);
					weibo.setRedirectUrl(AppCode.REDIRECT_URL);

					SharedPreferences sp = LogoActivity.this.getSharedPreferences(SharedPreferencesConfig.SP_NAME, SharedPreferencesConfig.MODE_WR);
					String accessToken = sp.getString(Strings.ACCESS_TOKEN, "");
					long expiresIn = sp.getLong(Strings.EXPIRES_TIME, 0);

					Intent intent = null;

					// 自动登录
					if (StringUtils.isNotBlank(accessToken) && expiresIn != 0 && (expiresIn > System.currentTimeMillis())) {
						weibo.setAccessToken(new AccessToken(accessToken, AppCode.CONSUMER_SECRET));
						intent = new Intent(LogoActivity.this, MainActivity.class);
					} else {
						// 非自动登录
						intent = new Intent(LogoActivity.this, StartActivity.class);
					}
					startActivity(intent);
					finish();
				}
			}));
		} else {
			MainService.alertNetError(LogoActivity.this);
		}
	}

}